﻿using System.Collections.Generic;

namespace Com.OpenCode.Framework.Core
{
    /// <summary>
    /// Behavior服务类。
    /// </summary>
    public static class BehaviorServices
    {

        private static List<IBehavior> _Behaviors = new List<IBehavior>();

        public static void Register(IBehavior behavior)
        {
            if (behavior != null)
            {
                if (_Behaviors.Find(p => p.Name == behavior.Name) == null)
                {
                    _Behaviors.Add(behavior);
                }
            }
        }

        public static void UnRegister(string name)
        {
            foreach (IBehavior behavior in _Behaviors)
            {
                if (behavior.Name == name)
                {
                    _Behaviors.Remove(behavior);
                    break;
                }
            }
        }

        public static void Dispatch(BehaviorEventArgs e)
        {
            foreach (IBehavior behavior in _Behaviors)
            {
                if (behavior.Name == e.Name)
                {
                    behavior.Action(e);
                }
            }
        }

        public static void Dispatch(string name, object parameter=null)
        {
            Dispatch(new BehaviorEventArgs
            {
                Name = name,
                Parameter = parameter
            });
        }

    }
}
